// 2013-10-6下午12:22:08  net.betou.core.service.impl.OpenTradePostDataListener.java

package net.betou.core.service.impl;

import java.util.Map;

import net.betou.common.util.DateUtils;
import net.betou.common.web.Constants;
import net.betou.core.service.PostDataAbstract;
import net.betou.core.service.post.PostKeyVal;
import net.betou.trade.entity.main.AgentAndAdmins;
import net.betou.trade.entity.main.Trade;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Connection.KeyVal;

/**
 * @author 叶装装
 */
public class OpenTradePostDataListener extends PostDataAbstract< Trade >{

	@Override
	public void afterChange( Trade bean, Map< String, Object > map ) {
		send( bean.getAdmin( ), bean, bean.getCategory( ).getName( ) );
	}

	public void afterSave( final Trade bean ) {

		final AgentAndAdmins admin = bean.getAdmin( );
		final String categoryName = bean.getCategory( ).getName( );

		KeyVal key = new PostKeyVal( mng ){
			@Override
			public String processValue( ) {

				StringBuffer buffer = new StringBuffer( );

				boolean agency = admin.getAgency( );

				// 发布标识
				this.appendToBuffer( buffer, "xiadan" );
				// 发布单位
				this.appendToBuffer( buffer, agency ? 1 : 0 );

				Integer [ ] agencyProxy = getAgencyProxy( admin );
				// 机构ID
				this.appendToBuffer( buffer, agencyProxy[ 0 ] );

				// 代理商ID
				this.appendToBuffer( buffer, agencyProxy[ 1 ] );

				// 发布人用户名
				this.appendToBuffer( buffer, admin.getUsername( ) );
				// 面向用户组
				this.appendToBuffer( buffer, bean.getOrientedGroup( ) );
				// 品种
				this.appendToBuffer( buffer, categoryName );
				// 方向
				this.appendToBuffer( buffer, bean.getDirection( ) ? "空单" : "多单" );
				// 开仓点位+止损点位+止盈点位+发布时间+理由
				this.appendToBuffer( buffer, bean.getOpenPosition( ) );
				this.appendToBuffer( buffer, bean.getStopPosition( ) );
				this.appendToBuffer( buffer, bean.getLongPosition( ) );
				this.appendToBuffer( buffer, DateUtils.getStringOnFormat( bean.getStartDate( ), 1 ) );
				this.appendToBuffer( buffer, this.subString( this.cleanHtml( bean.getOpenReason( ) ) ) );

				return buffer.toString( );

			}
		};

		post.setKeyVal( key );
		task.addTask( post );

		send( admin, bean, categoryName );
	}

	private void send( AgentAndAdmins admin, Trade bean, String categoryName ) {
		Boolean send = bean.getSend( );
		
		if( send == null || !send ){
			return;
		}

		String smsGroup = bean.getSmsGroups( );

		boolean validateSend = validateSmsSend( smsGroup, Constants.GET );

		if( !validateSend ){
			return;
		}
		smsPost.setAdmin( admin );
		smsPost.setSmsGroups( smsGroup );
		smsPost.setContent( getContent( bean, categoryName ) );
		task.addTask( smsPost );
	}

	/**
	 * 品种：润达银鼎，方向：空单，建仓价：16800，止损价：16700，止盈价：16900，时间：15:43:01
	 * 
	 * @param admin
	 * @param bean
	 * @return
	 */
	private String getContent( Trade bean, String categoryName ) {
		StringBuffer buffer = new StringBuffer( );

		appendTrade( buffer, "开仓品种", categoryName, false );
		appendTrade( buffer, "方向", bean.getDirection( ) ? "空单" : "多单", false );
		appendTrade( buffer, "开仓价", bean.getOpenPosition( ), false );
		appendTrade( buffer, "止损价", bean.getStopPosition( ), false );
		appendTrade( buffer, "止盈价", bean.getLongPosition( ), false );
		appendTrade( buffer, "时间", StringUtils.split( DateUtils.getStringOnFormat( bean.getStartDate( ), 1 ), " " )[ 1 ], true );

		return buffer.toString( );
	}
}
